###  Figure 1  ###

# Load packages 
library(readstata13)
library(ggplot2)
library(dplyr)

# Read Replication data 
data <- read.dta13("~/Dropbox/Replication_MVC/Datasets/datasets_analysis/panel_excombatientes.dta",
                   convert.factors = TRUE, generate.factors = FALSE,
                   encoding = "UTF-8", fromEncoding = NULL, convert.underscore = FALSE,
                   missing.type = FALSE, convert.dates = TRUE, replace.strl = TRUE,
                   add.rownames = FALSE, nonint.factors = TRUE, select.rows = NULL)


# Prep type of data
data$Type_of_Crime2 <- stringi::stri_trans_general(data$Type_of_Crime, "nfd")
data$Type_of_Crime2 <- stringi::stri_trans_general(data$Type_of_Crime2, "ascii")

# Collapse (sum) captures by Type_of_Crime
collapsed_data <- aggregate(diffcaptures ~ Type_of_Crime2, data = data, FUN = sum)

# Drop rows where Type_of_Crime2 is missing 
collapsed_data <- collapsed_data[!collapsed_data$Type_of_Crime2 == ".", ]

# Calculate sum of captures
sum_diffcaptures <- sum(collapsed_data$diffcaptures)

# Generate percent variable
collapsed_data$per2 <- collapsed_data$diffcaptures / sum_diffcaptures
collapsed_data$per2 <- collapsed_data$per2 * 100

collapsed_data <- collapsed_data %>%
    mutate(Type_of_Crime2 = ifelse(Type_of_Crime2 == "Illegal possession and trafficking of Military arms and ammunition",
                                   "Illegal use of military weapons", Type_of_Crime2))

collapsed_data <- collapsed_data %>%
    mutate(Type_of_Crime2 = ifelse(Type_of_Crime2 == "Illegal arms possession, trafficking or production",
                                   "Illegal use of weapons", Type_of_Crime2))


# Figure 1: Arrests by type of crime
g1 <- ggplot(collapsed_data, aes(x = per2, y = reorder(factor(Type_of_Crime2), per2))) +
    geom_segment(aes(yend = Type_of_Crime2, xend = 0), size = 4) +  
    xlab("Percentage Captures by Type") +
    ylab("") +
    theme(axis.text.y = element_text(size = 7),
          axis.title.x = element_text(size = 7)) +  
    scale_x_continuous(expand = c(0, 0))
###  Figure 1  ###
